package com.itheima.reggie.service.Impl;

import com.itheima.reggie.entity.User;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.UserMapper;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    //获取dao层对象
    @Autowired(required = false)
    private UserMapper userMapper;


    /**
     * 用户登录
     *
     * @param phone
     * @param userCode
     * @param sessionCode
     * @return
     */
    @Override
    public User login(String phone, String userCode, String sessionCode) {
        //首先判断验证码是否正确
        if (!userCode.equals(sessionCode)){
            throw new BusinessException("验证码不正确");
        }
        //调用dao层查询方法查询用户是否存在
        User login = userMapper.findByPhone(phone);
        if (login != null){
            if (login.getStatus() == 1){
                throw new BusinessException("您的账号已被禁用");
            }
        }else {
            //不为空,将手机号写入数据库
            User user = new User();
            user.setPhone(phone);
            user.setStatus(0);
            userMapper.save(user);
        }
        //返回用户
        return login;
    }
}
